// pages/evaluation/evaluationtest/evaluationtest.js


const app = getApp()
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {
    GetQuelist, //测评列表
    GetTestresultSubmit, // 提交测评结果
} from '../../../api/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
        listArray:[],
        selectIndex:0,
        model:null,
        selectArray:[],
        types:''
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.setData({
        types:options.types
      })
    this._GetQuelist("?types=" + options.types + "&queNum=120",true)
  },

  // 上一页
  upAction() {
    if (this.data.selectIndex == 0) {
        return
    }
    this.setData({
        model:this.data.listArray[this.data.selectIndex - 1],
        selectIndex:this.data.selectIndex - 1
    })
  },

  // 下一页
  downAction() {
      console.log(this.data.selectIndex)
      console.log(this.data.listArray.length)

      if (this.data.model.ansower == '' || this.data.model.ansower == null) {
        Toast('请确认选项')
        return
      }
      if (this.data.selectIndex + 1 == this.data.listArray.length) {
        let ansowerModel = {id:this.data.model.id,title:this.data.model.ansower}
        this.data.selectArray.push(ansowerModel)

        console.log("234")
        let that = this
        let openid = wx.getStorageSync('openId') || '';
        Dialog.confirm({
                title: '提交',
                message: '确认提交',
            })
            .then(() => {
                console.log({openId:openid,resultlist:that.data.selectArray})
                // on confirm
                that._GetTestresultSubmit({openId:openid,resultlist:that.data.selectArray,types:this.data.types}, true)
            })
            .catch(() => {
                // on cancel
            });
            return
    }
    
    let ansowerModel = {id:this.data.model.id,title:this.data.model.ansower}
    this.data.selectArray.push(ansowerModel)
    this.setData({
        model:this.data.listArray[this.data.selectIndex + 1],
        selectIndex:this.data.selectIndex + 1
    })
    
    
  },

  // 确认选择
  yesSelectAction(event) {
      this.setData({
          'model.ansower':"A",
         ['listArray['+ this.data.selectIndex + '].ansower'] : 'A'
      })
      if (this.data.selectIndex + 1 == this.data.listArray.length) {
          return
      }
     let ansowerModel = {id:this.data.model.id,title:this.data.model.ansower}
     this.data.selectArray.push(ansowerModel)
     this.setData({
        model:this.data.listArray[this.data.selectIndex + 1],
        selectIndex:this.data.selectIndex + 1
     })
  },

  // 否定选择
  noSelectAction(event) {
    this.setData({
        'model.ansower':"B",
        ['listArray['+ this.data.selectIndex + '].ansower'] : 'B'
      })
      if (this.data.selectIndex + 1 == this.data.listArray.length) {
        return
    }
    let ansowerModel = {id:this.data.model.id,title:this.data.model.ansower}
    this.data.selectArray.push(ansowerModel)
     this.setData({
        model:this.data.listArray[this.data.selectIndex + 1],
        selectIndex:this.data.selectIndex + 1
     })
  },


  _GetQuelist(data, isLoading) {
    console.log('列表')
    GetQuelist(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                listArray:res.data,
                model:res.data[0]
            })
        } else {
            Toast(res.msg)
        }
    })
},

//  提交测评结果
_GetTestresultSubmit(data, isLoading) {
    GetTestresultSubmit(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        let that = this;
        if (code == 200) {
            Toast({
                type: 'success',
                message: '提交成功',
                onClose: () => {
                    wx.redirectTo({
                        url: '/pages/evaluation/evaluationtestResult/evaluationtestResult?types=' + that.data.types,
                      })
                },
              });
            
        } else {
            Toast(res.msg)
        }
    })
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  onShareTimeline: function (){

},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})